<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-09-23 09:17
 *
 * 项目：rm  -  $  - PayController.php
 *
 * 作者：liwei 
 */

namespace modules\levpays\modules\baidupay\controllers;

use Lev;
use lev\base\Controllerv;
use lev\widgets\login\loginWidget;
use modules\levpays\modules\baidupay\sdk\baiduPayment;

!defined('INLEV') && exit('Access Denied LEV');


class PayController extends Controllerv
{

    public static function actionIndex() {
        echo '百度支付';
    }

    public static function actionNotify() {
        $type = Lev::stripTags(Lev::GPv('type'));
        if ($type && method_exists(__CLASS__, $method = 'action'.Lev::ucfirstv($type))) {
            static::$method();
        }else {
            static::actionPayNotify();
        }
    }

    //二维码授权登陆地址 - 打开百度小程序授权登陆页面
    public static function actionQrcodeAuth() {

    }

    //二维码支付地址 - 打开百度小程序支付页面
    public static function actionQrcodePay() {
        echo 'qrcode';
    }

    //退款回调地址
    public static function actionRefund() {

    }

    //退款审核地址
    public static function actionCheck() {

    }

    /**
     * 查询支付订单
     */
    public static function actionCheckTradeNo() {
        $tradeNo = Lev::stripTags(Lev::GPv('tradeNo'));
        $pre = Lev::stripTags(Lev::GPv('pre'));
        $res = baiduPayment::checkTradeNo($tradeNo);
        if ($res['_paySuccess']) {
            baiduPayment::paySuccess(['message'=>'已支付'], $tradeNo, $pre, $res);
            $location = loginWidget::getLoginReferer() ?: 1;
            echo json_encode(Lev::responseMsg(1, '支付成功', ['location'=>$location]));
        }elseif (strpos($res['msg'], '不存在') !== false || empty($res['data']['status']) || $res['data']['status'] ==1) {//未支付
            echo json_encode(Lev::responseMsg(2, '',[Lev::$app['isAdmin']? $res :'']));
        }else {
            echo json_encode(Lev::responseMsg(-2, $res['msg'].''.$res['errno'], [Lev::$app['isAdmin']? $res :'']));
        }
    }

    public static function actionCreateOrder() {
        $out_trade_no = Lev::stripTags(Lev::GPv('orderid'));
        $subject      = Lev::stripTags(Lev::GPv('subject'));
        $total_amount = Lev::stripTags(Lev::GPv('total_amount'));
        $body         = Lev::stripTags(Lev::GPv('body'));
        $result = baiduPayment::createOrder($out_trade_no, $subject, $total_amount, $body);
        echo json_encode($result);
    }

    /**
     * 文档： https://smartprogram.baidu.com/docs/develop/function/tune_up_notice_2.0/
     */
    public static function actionPayNotify() {
        echo '{"errno":0,"msg":"success","data":{"isConsumed":2}}';
    }

}